package com.tbit.uqbike.service.business.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSON;
import com.tbit.uqbike.object.pojo.AddCountFee;
import com.tbit.uqbike.service.baseAbstract.SynchronousService;
import com.tbit.uqbike.webmanager.dao.core.AddCountFeeDao;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * 同步计费规则实现类
 * @author <a href="mailto:liu.haoxiang@tbit.com.cn">BUGXiang</a>
 * @version 1.0
 * @since 2025-2-20
 */
@Service("billingRulesService")
@Slf4j
public class BillingRulesServiceImpl extends SynchronousService {

    @Resource
    private AddCountFeeDao addCountFeeDao;


    /**
     * 获取区域计费规则
     * @param accountId 区域id
     * @return 区域计费规则
     */
    @Override
    protected List<?> queryConfig(Integer accountId) {
        AddCountFee addCountFee = addCountFeeDao.getByAccountId(accountId);
        if (addCountFee != null) {
            return Collections.singletonList(addCountFee);
        } else {
            return Collections.emptyList();
        }
    }

    /**
     * 删除配置
     * @param accountId 区域id
     */
    @Override
    protected void deleteConfig(Integer accountId) {
        AddCountFee fee = addCountFeeDao.getByAccountId(accountId);
        if (Objects.nonNull(fee)){
            log.info("同步配置删除计费:{}", JSON.toJSONString(fee));
            addCountFeeDao.deleteByAccountId(accountId);
        }
    }


    /**
     * 插入区域计费规则
     * @param objects 配置列表
     * @param accountId 区域id
     */
    @Override
    protected void insertConfig(List<?> objects, Integer accountId) {
        if (CollectionUtil.isEmpty(objects)){
            return;
        }
        // 尝试将 List<?> 转换为 List<AddCountFee>
        List<AddCountFee> addCountFees = (List<AddCountFee>) objects;
        addCountFees.forEach(AddCountFee -> AddCountFee.setAccountId(accountId));
        addCountFeeDao.insert(addCountFees.get(0));
    }
}
